package com.demo.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
public class ChatClientController_02 {

    @Autowired
    private  ChatClient chatClient;

    @RequestMapping("/chat")
    public String chat(@RequestParam(value = "msg", defaultValue = "What is the meaning of life?") String msg) {

        return chatClient.prompt()
                .user(msg)//用户输入的信息
                .call()//调用大模型
                .content();//返回文本

    }

    /**
     * 流式处理
     * @param message
     * @return
     */
    @GetMapping(value = "/stream",produces="text/html;charset=UTF-8")
    public Flux<String> chatStream(@RequestParam(value = "msg") String message) {
        return chatClient.prompt().user(message).stream().content();
    }
}
